package com.fleafinder;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HomeScreenFragment extends Fragment {

	private GridView gridViewButton;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_screen_fragment, container,
				false);
		
		final DataCenter dc = DataCenter.getInstance();
		
		this.gridViewButton = (GridView) view.findViewById(R.id.gridViewButton);
		this.gridViewButton.setAdapter(new GridViewButtonAdapter(view
				.getContext()));
		this.gridViewButton.setVerticalScrollBarEnabled(false);
		
		View.OnClickListener listener = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dc.setCurrentMarketListMode(CommonConstants.ALL_MARKETTYPE);
				dc.setSelectedMaketCategory(-1);
				FragmentActivity activity = (FragmentActivity) getActivity();
				FragmentManager fm = activity.getSupportFragmentManager();
				FragmentTransaction transaction = fm.beginTransaction();
				transaction.replace(R.id.fragment_placeholder,
						dc.getMarketListFragment(),
						CommonConstants.HOME_FRAGMENT);
				transaction.addToBackStack(CommonConstants.HOME_FRAGMENT);
				transaction.commit();
			}
		};
		View buttonView = inflater.inflate(R.layout.grid_button_layout, null);
		
		RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
		        ViewGroup.LayoutParams.WRAP_CONTENT);
		p.width = (int)(dc.getScreenSize().x * 0.447);
		p.height = (int)(p.width * 0.325);

		final ImageView btnImageView = (ImageView) buttonView.findViewById(R.id.grid_button_image);
		btnImageView.setOnClickListener(listener);
		btnImageView.setLayoutParams(p);
		final TextView btnTextView = (TextView)buttonView.findViewById(R.id.grid_button_text);
		btnTextView.setText(CommonConstants.VIEW_ALL);
		btnTextView.setOnClickListener(listener);
		
		ViewGroup vg = (ViewGroup)view.findViewById(R.id.homeButtonVisAlleContainer);
		
		vg.addView(buttonView);
		
		return view;
	}
}
